﻿package mortal.game.control
{
    import Message.Public.*;
    import extend.language.*;
    import mortal.common.global.*;
    import mortal.common.net.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.guide.*;
    import mortal.game.view.mainUI.avatar.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class AvatarController extends Controller
    {
        private var _avatarModule:AvatarModule;
        private var isNeedUpdate:Boolean = false;

        public function AvatarController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (!this._avatarModule)
            {
                this._avatarModule = new AvatarModule();
            }
            return this._avatarModule;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BufferUpdate, this.upDateBuffLatter);
            NetDispatcher.addCmdListener(ServerCommand.CaptainChange, this.captainChange);
            NetDispatcher.addCmdListener(ServerCommand.FightSetModeSuccess, this.updateFightMode);
            NetDispatcher.addCmdListener(ServerCommand.DrugUpdata, this.refreshDrugNum);
            NetDispatcher.addCmdListener(ServerCommand.SystemClientSettingGot, this.setSliderValue);
            NetDispatcher.addCmdListener(ServerCommand.IssmResult, this.updateIssm);
            NetDispatcher.addCmdListener(ServerCommand.IssmUpdate, this.sIssmHandler);
            NetDispatcher.addCmdListener(ServerCommand.VipRewardGet, this.vipBtnFilter);
            NetDispatcher.addCmdListener(ServerCommand.VipLevelUp, this.vipLevelUp);
            NetDispatcher.addCmdListener(ServerCommand.GoldUpdate, this.goldUpdate);
            NetDispatcher.addCmdListener(ServerCommand.VipFreeInfoUpdate, this.showVipExperienceBtn);
            Dispatcher.addEventListener(EventName.Scene_Update, this.updateFightMode);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.onLoginGame);
            Dispatcher.addEventListener(EventName.FightSetMode, this.setFightMode);
            Dispatcher.addEventListener(EventName.AvatarModuleVisible, this.onVisible);
            Dispatcher.addEventListener(EventName.RoleCareerChanged, this.roleCareerChangedHandler);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.addMinyClientBuff);
            return;
        }// end function

        private function addMinyClientBuff(event:DataEvent) : void
        {
            if (ParamsConst.instance.isMiniCliet)
            {
                BuffUtil.addBuffByCode(23000012);
            }
            return;
        }// end function

        private function onVisible(event:DataEvent) : void
        {
            this._avatarModule.visible = event.data;
            return;
        }// end function

        private function setFightMode(event:DataEvent) : void
        {
            if (Cache.instance.role.entityInfo.level < GameConst.FightModeLevelLimit)
            {
                return MsgManager.showRollTipsMsg(Language.getStringByParam(20382, GameConst.FightModeLevelLimit));
            }
            if (Cache.instance.guild.isInGuild && !Cache.instance.guild.isInSelfGuild)
            {
                return MsgManager.showRollTipsMsg(Language.getString(20383));
            }
            GameProxy.sceneProxy.changeFightMode(event.data as int);
            return;
        }// end function

        private function onLoginGame(event:DataEvent) : void
        {
            this._avatarModule.updateEntity();
            return;
        }// end function

        private function updateFightMode(param1:Object) : void
        {
            this._avatarModule.setModeStyle(param1 as int);
            return;
        }// end function

        private function upDateBuffLatter(param1:Object = null) : void
        {
            this.isNeedUpdate = true;
            CallLater.setCallLater(this.updateBuffs, 0.5);
            return;
        }// end function

        private function updateBuffs(param1:Object) : void
        {
            var _loc_2:Array = null;
            if (this.isNeedUpdate)
            {
                this.isNeedUpdate = false;
                _loc_2 = Cache.instance.buff.showBuffArray;
                this._avatarModule.updateBufferByBuffInfoArray(_loc_2);
            }
            return;
        }// end function

        private function captainChange(param1:Object = null) : void
        {
            if (this._avatarModule)
            {
                this._avatarModule.captainChange();
            }
            return;
        }// end function

        private function refreshDrugNum(param1:Object = null) : void
        {
            if (this._avatarModule)
            {
                this._avatarModule.refreshDrugNum();
            }
            return;
        }// end function

        private function setSliderValue(param1:Object = null) : void
        {
            this._avatarModule.setSliderValue();
            return;
        }// end function

        private function updateIssm(param1:Object = null) : void
        {
            this._avatarModule.updateIssm(int(param1));
            return;
        }// end function

        private function sIssmHandler(param1:Object) : void
        {
            this._avatarModule.updateIssm();
            return;
        }// end function

        private function updateCd(param1:Object = null) : void
        {
            this._avatarModule.updateCd();
            return;
        }// end function

        private function vipBtnFilter(param1:Object) : void
        {
            if (this._avatarModule)
            {
                this._avatarModule.vipBtnFliter();
            }
            return;
        }// end function

        private function vipLevelUp(param1:Object) : void
        {
            if (this._avatarModule)
            {
                this._avatarModule.vipBtnFliter();
                this._avatarModule.updateVipLevel();
            }
            return;
        }// end function

        private function goldUpdate(param1:Object) : void
        {
            if (this._avatarModule)
            {
                this._avatarModule.updateMoney();
            }
            return;
        }// end function

        public function showVipExperienceBtn(param1:Object) : void
        {
            var _loc_2:* = param1 as SVipInfo;
            if (this._avatarModule)
            {
                if (_loc_2 && _loc_2.expiredDt && Cache.instance.vip.level < 0)
                {
                    this._avatarModule.showVipExperienceBtn((_loc_2.expiredDt.time - ClockManager.instance.nowDate.time) * 0.001);
                }
                else
                {
                    this._avatarModule.hideVipExperienceBtn();
                }
                this._avatarModule.vipBtnFliter();
            }
            return;
        }// end function

        public function roleCareerChangedHandler(event:DataEvent) : void
        {
            if (this._avatarModule && AutoGuideController.isUIRowIninted)
            {
                this._avatarModule.updateAvatar();
                this._avatarModule.updateCarrer();
            }
            return;
        }// end function

    }
}
